package tech.zhiwei.frostmetal.modules.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.zhiwei.frostmetal.core.base.common.P;
import tech.zhiwei.frostmetal.core.base.common.PageParam;
import tech.zhiwei.frostmetal.core.base.common.R;
import tech.zhiwei.frostmetal.modules.demo.dto.DemoDTO;
import tech.zhiwei.frostmetal.modules.demo.entity.Demo;
import tech.zhiwei.frostmetal.modules.demo.service.IDemoService;
import tech.zhiwei.frostmetal.modules.demo.vo.DemoVO;
import tech.zhiwei.frostmetal.modules.demo.wrapper.DemoWrapper;
import tech.zhiwei.tool.lang.StringUtil;
import tech.zhiwei.tool.lang.ObjectUtil;

import java.util.Collection;
import java.util.List;

/**
 * 生成代码示例1 Controller
 *
 * @author ALIEN 
 * @since 2024/10/10
 */
@RestController
@RequestMapping("/demo")
@AllArgsConstructor
@Tag(name = "demo", description = "生成代码示例1API")
public class DemoController {
    private IDemoService demoService;

    @PostMapping
    @Operation(summary = "新增或更新生成代码示例1", operationId = "saveDemo")
    public R<Long> save(@RequestBody DemoDTO DemoDTO) {
        return R.data(demoService.saveDemo(DemoDTO));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询生成代码示例1", operationId = "demoPage")
    public P<List<DemoVO>> page(@ParameterObject PageParam pageParam
            , @RequestParam(required = false) String a
            , @RequestParam(required = false) String b
            , @RequestParam(required = false) String d
            , @RequestParam(required = false) String e
            , @RequestParam(required = false) String f
            , @RequestParam(required = false) String g
            , @RequestParam(required = false) String h
            , @RequestParam(required = false) String i
            , @RequestParam(required = false) String j
    ) {
        LambdaQueryWrapper<Demo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtil.isNotNull(a), Demo::getA, a);
        queryWrapper.like(ObjectUtil.isNotNull(b), Demo::getB, b);
        queryWrapper.ne(ObjectUtil.isNotNull(d), Demo::getD, d);
        queryWrapper.gt(ObjectUtil.isNotNull(e), Demo::getE, e);
        queryWrapper.ge(ObjectUtil.isNotNull(f), Demo::getF, f);
        queryWrapper.lt(ObjectUtil.isNotNull(g), Demo::getG, g);
        queryWrapper.le(ObjectUtil.isNotNull(h), Demo::getH, h);
        queryWrapper.likeLeft(ObjectUtil.isNotNull(i), Demo::getI, i);
        queryWrapper.likeRight(ObjectUtil.isNotNull(j), Demo::getJ, j);

        return P.page(DemoWrapper.getInstance().pageVO(demoService.page(queryWrapper, pageParam)));
    }

    @GetMapping("/list")
    @Operation(summary = "查询所有生成代码示例1", operationId = "demoList")
    public R<List<DemoVO>> list() {
        return R.data(DemoWrapper.getInstance().listVO(demoService.list()));
    }

    @GetMapping("/{id}")
    @Operation(summary = "生成代码示例1详情", operationId = "demoDetail")
    @Parameter(name = "id", description = "记录id")
    public R<DemoVO> detail(@PathVariable Long id) {
        return R.data(DemoWrapper.getInstance().entityVO(demoService.getById(id)));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "单个删除生成代码示例1", operationId = "deleteDemo")
    @Parameter(name = "id", description = "记录id")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.status(demoService.remove(id));
    }

    @DeleteMapping
    @Operation(summary = "批量删除生成代码示例1", operationId = "deleteDemos")
    public R<Boolean> delete(@RequestBody Collection<Long> ids) {
        return R.status(demoService.remove(ids));
    }
}
